package com.derek.omshopping.ui.home.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.derek.omshopping.R;
import com.derek.omshopping.bean.home.HomeIndexListAllBean;
import com.derek.omshopping.bean.home.HomeMainTypeBean;
import com.derek.omshopping.bean.home.HomeVideoBean;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.List;

/**
 * Created by duyb on 2016-10-08.
 * Desc: 多布局adapter
 */

public class FragmentHomeTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //定义类型常量
    private final int TYPE1 = 1;
    private final int TYPE2 = 2;
    private final int TYPE3 = 3;
    private final int TYPE4 = 4;
    private Context mContext;
    private LayoutInflater mInflater;
    private List<HomeMainTypeBean> list;
    private List<HomeVideoBean> videoList;
    private List<HomeIndexListAllBean> indexList;

    public FragmentHomeTypeAdapter(Context mContext) {
        this.mContext = mContext;
        this.mInflater = LayoutInflater.from(mContext);
    }


    //视频数据
    public void setData(List<HomeMainTypeBean> list) {
        this.list = list;
    }

    //视频数据
    public void setVideoData(List<HomeVideoBean> list) {
        this.videoList = list;
        notifyDataSetChanged();
    }

    //列表
    public void setIndexData(List<HomeIndexListAllBean> list) {
        this.indexList = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE1) {
            view = mInflater.inflate(R.layout.item_type_home_viewpage, parent, false);
            return new ViewPageHolder(view);
        } else if (viewType == TYPE2) {
            view = mInflater.inflate(R.layout.item_type_home_button, parent, false);
            return new ButtonHolder(view);
        } else if (viewType == TYPE3) {
            view = mInflater.inflate(R.layout.item_type_home_videos, parent, false);
            return new BroadcastHolder(view);
        } else if (viewType == TYPE4) {
            view = mInflater.inflate(R.layout.item_type_home_index, parent, false);
            return new HomeIndexHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FragmentHomeTypeAdapter.ViewPageHolder) {
            ((FragmentHomeTypeAdapter.ViewPageHolder) holder).rollPagerView.setAdapter(new HomeViewPageAdapter(mContext, list.get(4).getSetList().get(position).getContentList()));
        } else if (holder instanceof FragmentHomeTypeAdapter.ButtonHolder) {
            ((ButtonHolder) holder).recyclerView.setLayoutManager(new GridLayoutManager(mContext, 5));
            ((ButtonHolder) holder).recyclerView.setAdapter(new HomeButtonAdapter(mContext, list.get(1).getSetList()));
        } else if (holder instanceof FragmentHomeTypeAdapter.BroadcastHolder) {
            ((BroadcastHolder) holder).recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            ((BroadcastHolder) holder).recyclerView.setAdapter(new HomeVideoAdapter(mContext, videoList));

        } else if (holder instanceof FragmentHomeTypeAdapter.HomeIndexHolder) {
            ((HomeIndexHolder) holder).recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            ((HomeIndexHolder) holder).recyclerView.setAdapter(new HomeIndexAdapter(mContext, indexList));
        }
    }

    @Override
    public int getItemCount() {
        // return list.size();
        return 4;
    }

    //positon代表item在recycleview中的位置 返回int值控制item要显示的布局
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE1;
        } else if (position == 1) {
            return TYPE2;
        } else if (position == 2) {
            return TYPE3;
        } else if (position == 3) {
            return TYPE4;
        }
        return super.getItemViewType(position);
    }

    class ViewPageHolder extends RecyclerView.ViewHolder {
        private RollPagerView rollPagerView;

        public ViewPageHolder(View itemView) {
            super(itemView);
            rollPagerView = (RollPagerView) itemView.findViewById(R.id.simpleSlider_viewPage);
            rollPagerView.setHintView(new ColorPointHintView(mContext, Color.RED, Color.WHITE));

        }
    }

    class ButtonHolder extends RecyclerView.ViewHolder {
        private RecyclerView recyclerView;

        public ButtonHolder(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.button_recycleview);
        }
    }

    class HomeIndexHolder extends RecyclerView.ViewHolder {
        private RecyclerView recyclerView;

        public HomeIndexHolder(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.index_recycleview);
        }
    }

    class BroadcastHolder extends RecyclerView.ViewHolder {
        private RecyclerView recyclerView;

        public BroadcastHolder(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.video_recycleview);
        }
    }
}
